/*
 * @(#)DownloadServlet.java Time: 2013-2-28
 *
 * Copyright 2013 xuedou.com All rights reserved.
 */
package xuedou.skymoni.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import common.Logger;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import xuedou.skymoni.bean.Exam;
import xuedou.skymoni.impl.ExamImpl;
import xuedou.skymoni.service.ExamService;

/**
 *<pre>类说明</pre>
 *<b>功能描述：</b>
 * 下载Excel
 * @author  jinmingming jinmingming@xuedou.com
 * @version 1.0, 2013-2-28
 */
public class DownloadServlet extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		ExamService examService = new ExamImpl();
		OutputStream os=null;
		try 
		{
			os = response.getOutputStream();
		} 
		catch (IOException e1) 
		{
			Logger.getLogger(DownloadServlet.class).error("获取response输出流出错");
		}
		
	    response.reset();
	    response.setContentType("application/msexcel");
	    String fileName="布外模拟考信息数据.xls";
		response.setHeader("Content-Disposition", "attachment;"+ " filename="+ new String(fileName.getBytes(), "ISO-8859-1"));
		WritableWorkbook wwb=null;
		WritableSheet ws=null;
		try 
		{
			wwb = Workbook.createWorkbook(os);
			ws=wwb.createSheet("布外模拟考信息数据",0);
			ws.getSettings().setDefaultColumnWidth(15);
			//创建表头
			WritableFont wfc = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
            WritableCellFormat wcfFC = new WritableCellFormat(wfc);
            Label topid = new Label(0,0,"编号",wcfFC);
			Label topsname = new Label(1,0,"姓名",wcfFC);
			Label topschool = new Label(2,0,"学校",wcfFC);
			Label toptel = new Label(3,0,"手机",wcfFC);
			Label topissky = new Label(4,0,"是否在蓝天就读",wcfFC);
			Label topexamschool = new Label(5,0,"考试校区",wcfFC);
			Label topexamclassroom = new Label(6,0,"考试教室",wcfFC);
			Label topexamnum = new Label(7,0,"考试场次",wcfFC);
			Label topticknum = new Label(8,0,"准考证号",wcfFC);
			//2013/4/5
			Label topsnumber = new Label(9,0,"学生编号",wcfFC);
			Label topispayment = new Label(10,0,"是否支付",wcfFC);
			
			ws.addCell(topid);
			ws.addCell(topsname);
			ws.addCell(topschool);
			ws.addCell(toptel);
			ws.addCell(topissky);
			ws.addCell(topexamschool);
			ws.addCell(topexamclassroom);
			ws.addCell(topexamnum);
			ws.addCell(topticknum);
			//2013/4/5
			ws.addCell(topsnumber);
			ws.addCell(topispayment);
			
			Label id = null;
	        Label sname = null;
			Label school = null;
			Label tel = null;
			Label issky = null;
			Label examschool = null;
			Label examclassroom = null;
			Label examnum = null;
			Label ticknum = null;
			//2013/4/5
			Label snumber = null;
			Label ispayment = null;
			
			List<Exam> list = examService.allExam();
			int listsize = list.size();
            for (int i = 1; i <= listsize; i++){
				Exam exam = list.get(i-1);
				
				id = new Label(0,i,Integer.toString(i));
				sname = new Label(1,i,exam.getSname());
				school = new Label(2,i,exam.getSchool());
				tel = new Label(3,i,exam.getTel());
				issky = new Label(4,i,exam.getIssky());
				examschool = new Label(5,i,exam.getExamschool());
				examclassroom = new Label(6,i,exam.getClassRoom());
				examnum = new Label(7,i,exam.getExamnum());
				ticknum = new Label(8,i,exam.getTicknum());
				//2013/4/5
				snumber = new Label(9,i,exam.getSnumber());
				ispayment = new Label(10,i,exam.getIspayment());
				
				ws.addCell(id);
				ws.addCell(sname);
				ws.addCell(school);
				ws.addCell(tel);
				ws.addCell(issky);
				ws.addCell(examschool);
				ws.addCell(examclassroom);
				ws.addCell(examnum);
				ws.addCell(ticknum);
				//2013/4/5
				ws.addCell(snumber);
				ws.addCell(ispayment);
                
            }
		} 
		catch (Exception e) 
		{
			Logger.getLogger(DownloadServlet.class).error("输出Excel失败");
		}
		finally
		{
			try 
			{
				wwb.write();
				wwb.close();
				os.close();
			} 
			catch (WriteException e) 
			{
				Logger.getLogger(DownloadServlet.class).error("关闭WritableWorkbook出错");
			} 
			catch (IOException e) 
			{
				Logger.getLogger(DownloadServlet.class).error("关闭WritableWorkbook出错");
			}
		}
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
}
